package com.example.cofeed.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.activity.PushSubscriptionActivity;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Catalogs;
import com.example.cofeed.entity.City;
import com.lzy.okgo.model.Response;

/**
 * Created by mqwang on 2017/11/15.
 */

public class PushSubscriptionAdapter extends BaseQuickAdapter<Catalogs, BaseViewHolder> {
    private int type;

    public PushSubscriptionAdapter() {
        super(R.layout.item_push_subscription_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, final Catalogs item) {
        if (item.isSelect) {
            helper.getView(R.id.rec_lv).setVisibility(View.VISIBLE);
            helper.setImageResource(R.id.select_iv, R.mipmap.ic_up);
        } else {
            helper.getView(R.id.rec_lv).setVisibility(View.GONE);
            helper.setImageResource(R.id.select_iv, R.mipmap.ic_down);
        }

        helper.setText(R.id.name_tv, item.title);

        CityAdapter cityAdapter = new CityAdapter();
        RecyclerView recLv = helper.getView(R.id.rec_lv);
        recLv.setLayoutManager(new LinearLayoutManager(mContext));
        recLv.setAdapter(cityAdapter);
        cityAdapter.setType(type);
        cityAdapter.setNewData(item.citys);

        cityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final City city = (City) adapter.getData().get(position);
                ((CityAdapter) adapter).setSelect(city);
                if (mContext instanceof PushSubscriptionActivity) {
                    if (city.ispush == 1) {
                        ApiService.pushSubscriptionSet("pushSubscriptionSet", city.cityid, 1, new DialogCallback<LzyResponse<Object>>() {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
//                                city.ispush = 1;
//                                notifyDataSetChanged();
                            }
                        });
                    } else {
                        ApiService.pushSubscriptionSet("pushSubscriptionSet", city.cityid, 0, new DialogCallback<LzyResponse<Object>>() {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
//                                city.ispush = 0;
//                                notifyDataSetChanged();
                            }
                        });
                    }
                }
                //后期看下
//                else if (mContext instanceof SubscriptionCenterActivity) {
//                    ApiService.cartAdd(this, item.catalogid, city.cityid, city.ispush, new DialogCallback<LzyResponse<Object>>() {
//                        @Override
//                        public void onSuccess(Response<LzyResponse<Object>> response) {
////                            if (city.ispush == 0)
////                                city.ispush = 1;
////                            else
////                                city.ispush = 0;
////                            notifyDataSetChanged();
//                        }
//                    });
//                }

            }
        });
    }


    public void setSelect(Catalogs select) {
        select.isSelect = !select.isSelect;
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
    }

}
